package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Role;
import com.huidao.util.JDBCUtil;

public class RoleDaoImpl implements RoleDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	public boolean addRole(Role role) {
		String sql = "INSERT INTO ROLE(ROLE_NAME)VALUES(?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1,role.getRoleName());
			int i = ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO �Զ���ɵ� catch ��
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean delRole(Long id) {
		String sql = "DELETE FROM ROLE WHERE ROLE_ID";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i = ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO �Զ���ɵ� catch ��
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean updateRole(Role role) {
		// TODO �Զ���ɵķ������
		String sql = "UPDATE ROLE SET ROLE_NAME=? WHERE ROLE_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, role.getRoleName());
			ps.setLong(2,role.getRoleId());
			int i = ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO �Զ���ɵ� catch ��
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public List<Role> getListPage(int currentPage, int pageSize, String namekey) {
		List<Role> list = new ArrayList<Role>();
		String sql = "SELECT * FROM ROLE WHERE 1=1";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql+" AND ROLE_NAME LIKE '%"+namekey+"%'";
		}
		sql = sql+" LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1,(currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while(rs.next()){
				Role b = new Role();
				b.setRoleId(rs.getLong("ROLE_ID"));
				b.setRoleName(rs.getString("ROLE_NAME"));
				list.add(b);
			}
		} catch (SQLException e) {
			// TODO �Զ���ɵ� catch ��
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	public int getTotalCount(String namekey) {
		// TODO �Զ���ɵķ������
		String sql = "SELECT COUNT(*) FROM ROLE WHERE 1=1";
		if(namekey!=null&&!namekey.trim().equals("")){
			sql = sql +" AND ROLE_NAME LIKE '%"+namekey+"%' ";
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	public List<Role> getAllList() {
		// TODO �Զ���ɵķ������
		List<Role> list = new ArrayList<Role>();
		String sql = "SELECT * FROM ROLE ";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			while(rs.next()){
				Role b = new Role();
				b.setRoleId(rs.getLong("ROLE_ID"));
				b.setRoleName(rs.getString("ROLE_NAME"));
				list.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public boolean addRoleFunction(Long roleId, String[] menuIds) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO FUNCTION(ROLE_ID,MENU_ID)VALUES(?,?)";
		boolean isSuccess = true;
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			
			for(String menuId:menuIds){
				ps.setLong(1, roleId);
				ps.setLong(2, Long.parseLong(menuId));
				int i = ps.executeUpdate();
				if(i<=0){
					isSuccess = false;
				}
			}
			
		} catch (SQLException e) {
			isSuccess = false;
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return isSuccess;
		
	}

	@Override
	public boolean delRoleFunction(Long roleId) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM FUNCTION WHERE ROLE_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, roleId);
			rs =ps.executeQuery();
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean checkRoleFunction(Long roleId) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM FUNCTION WHERE ROLE_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, roleId);
			int i =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean isRoleFunction(Long roleId, String url) {
		String sql = "SELECT * FROM FUNCTION A " +
				"LEFT JOIN MENU B ON A.MENU_ID=B.MENU_ID " +
				"WHERE A.ROLE_ID=? AND B.MENU_URL LIKE ? ";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, roleId);
			ps.setString(2, url+"/%");
			rs =ps.executeQuery();
			if(rs.next()){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

}
